<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Controller Scripts - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.view.controllers.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.view.controllers.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.view.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.view.html">Zend_View</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.view.scripts.html">View Scripts</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.view.controllers" class="section"><div class="info"><h1 class="title">Controller Scripts</h1></div>
    

    <p class="para">
        The controller is where you instantiate and configure <span class="classname">Zend_View</span>.
        You then assign variables to the view, and tell the view to
        render output using a particular script.
    </p>

    <div class="section" id="zend.view.controllers.assign"><div class="info"><h1 class="title">Assigning Variables</h1></div>
        

        <p class="para">
            Your controller script should assign necessary variables to the view
            before it hands over control to the view script. Normally, you
            can do assignments one at a time by assigning to property names
            of the view instance:
        </p>

        <pre class="programlisting brush: php">
$view = new Zend_View();
$view-&gt;a = &quot;Hay&quot;;
$view-&gt;b = &quot;Bee&quot;;
$view-&gt;c = &quot;Sea&quot;;
</pre>


        <p class="para">
            However, this can be tedious when you have already collected the
            values to be assigned into an array or object.
        </p>

        <p class="para">
            The assign() method lets you assign from an array or object &quot;in
            bulk&quot;. The following examples have the same effect as the above
            one-by-one property assignments.
        </p>

        <pre class="programlisting brush: php">
$view = new Zend_View();

// assign an array of key-value pairs, where the
// key is the variable name, and the value is
// the assigned value.
$array = array(
    &#039;a&#039; =&gt; &quot;Hay&quot;,
    &#039;b&#039; =&gt; &quot;Bee&quot;,
    &#039;c&#039; =&gt; &quot;Sea&quot;,
);
$view-&gt;assign($array);

// do the same with an object&#039;s public properties;
// note how we cast it to an array when assigning.
$obj = new StdClass;
$obj-&gt;a = &quot;Hay&quot;;
$obj-&gt;b = &quot;Bee&quot;;
$obj-&gt;c = &quot;Sea&quot;;
$view-&gt;assign((array) $obj);
</pre>


        <p class="para">
            Alternatively, you can use the assign method to assign
            one-by-one by passing a string variable name, and then the
            variable value.
        </p>

        <pre class="programlisting brush: php">
$view = new Zend_View();
$view-&gt;assign(&#039;a&#039;, &quot;Hay&quot;);
$view-&gt;assign(&#039;b&#039;, &quot;Bee&quot;);
$view-&gt;assign(&#039;c&#039;, &quot;Sea&quot;);
</pre>

    </div>

    <div class="section" id="zend.view.controllers.render"><div class="info"><h1 class="title">Rendering a View Script</h1></div>
        

        <p class="para">
            Once you have assigned all needed variables, the controller
            should tell <span class="classname">Zend_View</span> to render a particular view script.
            Do so by calling the render() method. Note that the method will
            return the rendered view, not print it, so you need to print or
            echo it yourself at the appropriate time.
        </p>

        <pre class="programlisting brush: php">
$view = new Zend_View();
$view-&gt;a = &quot;Hay&quot;;
$view-&gt;b = &quot;Bee&quot;;
$view-&gt;c = &quot;Sea&quot;;
echo $view-&gt;render(&#039;someView.php&#039;);
</pre>

    </div>

    <div class="section" id="zend.view.controllers.script-paths"><div class="info"><h1 class="title">View Script Paths</h1></div>
        

        <p class="para">
            By default, <span class="classname">Zend_View</span> expects your view scripts to be relative to
            your calling script. For example, if your controller script is at
            &quot;/path/to/app/controllers&quot; and it calls
            $view-&gt;render(&#039;someView.php&#039;), <span class="classname">Zend_View</span> will look for
            &quot;/path/to/app/controllers/someView.php&quot;.
        </p>

        <p class="para">
            Obviously, your view scripts are probably located elsewhere. To
            tell <span class="classname">Zend_View</span> where it should look for view scripts, use the
            setScriptPath() method.
        </p>

        <pre class="programlisting brush: php">
$view = new Zend_View();
$view-&gt;setScriptPath(&#039;/path/to/app/views&#039;);
</pre>


        <p class="para">
            Now when you call $view-&gt;render(&#039;someView.php&#039;), it will look
            for &quot;/path/to/app/views/someView.php&quot;.
        </p>

        <p class="para">
            In fact, you can &quot;stack&quot; paths using the addScriptPath()
            method. As you add paths to the stack, <span class="classname">Zend_View</span> will look
            at the most-recently-added path for the requested view
            script. This allows you override default views with custom
            views so that you may create custom &quot;themes&quot; or &quot;skins&quot; for
            some views, while leaving others alone.
        </p>

        <pre class="programlisting brush: php">
$view = new Zend_View();
$view-&gt;addScriptPath(&#039;/path/to/app/views&#039;);
$view-&gt;addScriptPath(&#039;/path/to/custom/&#039;);

// now when you call $view-&gt;render(&#039;booklist.php&#039;), Zend_View will
// look first for &quot;/path/to/custom/booklist.php&quot;, then for
// &quot;/path/to/app/views/booklist.php&quot;, and finally in the current
// directory for &quot;booklist.php&quot;.
</pre>


        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Never use user input to set script paths</b><br /></span>
            

            <p class="para">
                <span class="classname">Zend_View</span> uses script paths to lookup and render
                view scripts. As such, these directories should be known
                before-hand, and under your control. <em class="emphasis">Never</em>
                set view script paths based on user input, as you can
                potentially open yourself up to Local File Inclusion
                vulnerability if the specified path includes parent directory
                traversals. For example, the following input could trigger the
                issue:
            </p>

            <pre class="programlisting brush: php">
// $_GET[&#039;foo&#039;] == &#039;../../../etc&#039;
$view-&gt;addScriptPath($_GET[&#039;foo&#039;]);
$view-&gt;render(&#039;passwd&#039;);
</pre>


            <p class="para">
                While this example is contrived, it does clearly show the
                potential issue. If you <em class="emphasis">must</em> rely on user
                input to set your script path, properly filter the input and
                check to ensure it exists under paths controlled by your
                application.
            </p>
        </p></blockquote>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.view.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.view.html">Zend_View</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.view.scripts.html">View Scripts</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.view.html">Zend_View</a></li>
  <li><a href="zend.view.introduction.html">Introduction</a></li>
  <li class="active"><a href="zend.view.controllers.html">Controller Scripts</a></li>
  <li><a href="zend.view.scripts.html">View Scripts</a></li>
  <li><a href="zend.view.helpers.html">View Helpers</a></li>
  <li><a href="zend.view.abstract.html">Zend_View_Abstract</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>